package com.adong.conditional;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author adong
 */

public class MyConditional implements Condition {
    /**
     * ConditionContext：判断条件能使用的上下文（环境）
     * AnnotatedTypeMetadata：注释信息
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        /*TODO是否linux系统
        1、能获取到ioc使用的beanfactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        2、获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        3、获取当前环境信息
        Environment environment = context.getEnvironment();
        获取当前操作系统的名字
        String property = environment.getProperty("os.name");
        4、获取到bean定义的注册类*/
        BeanDefinitionRegistry registry = context.getRegistry();
        //可以判断容器中的bean注册情况，也可以给容器中注册bean
        boolean definition = registry.containsBeanDefinition("pdd");
        //判断有没有pdd这
        if (definition) {
            return true;
        }
        return false;
    }
}
